//Flutter 本地存储
//和Android、Ios类似，Flutter也支持Preferences（Shared Preferences and NSUserDefaults） 、文件、和Sqlite3。
//只不过要想使用这个功能需要引入官方仓库的相应插件，那么我们就分别来看下这三种存储方式的使用方法。

//1: preference 存储
//Flutter中本身并不支持Preferences存储，需要借助于第三发的组件来实现。
//打开 https://github.com/flutter/plugins

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_practice/utils/Utils.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyPreferenceDemo extends StatelessWidget {
  final String _userName = 'username';

  final _userNamerController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    //保存
    save() async {
      SharedPreferences preferences = await SharedPreferences.getInstance();
      await preferences.setString(
          _userName, _userNamerController.value.text.toString());
    }

    //获取
    Future<String> get() async {
      var userName;
      SharedPreferences preferences = await SharedPreferences.getInstance();
      userName = await preferences.get(_userName);
      return userName;
    }

    return Scaffold(
      appBar: Utils.getAppBar('共享参数'),
      body: Column(
        children: <Widget>[
          TextField(
            decoration: InputDecoration(
              contentPadding: EdgeInsets.only(top: 10.0),
              icon: Icon(Icons.perm_identity),
              labelText: '请输入用户名',
            ),
            controller: _userNamerController,
          ),
          RaisedButton(
            onPressed: () {
              save();
              print('存储成功');
            },
            child: Text('存储'),
            color: Colors.red,
          ),
          RaisedButton(
            onPressed: () {
              Future<String> userName = get();
              userName.then((String userNamer) {
                print('获取成功' + userNamer);
              });
            },
            child: Text('获取'),
            color: Colors.blue,
          ),
        ],
      ),
    );
  }
}
